package org.xiaov.lunasql.engine.ast.adjust;

import com.alibaba.druid.sql.ast.expr.SQLIntervalExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntervalUnit;
import org.xiaov.lunasql.engine.ast.IntervalAdjust;
import org.xiaov.lunasql.engine.transform.interval.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * OracleIntervalAdjust
 * </p>
 *
 * @author lzq
 * @since 2024/12/24 16:18
 */
public class OracleIntervalAdjust implements IntervalAdjust {
    /**
     * interval和handler的映射
     */
    public static Map<SQLIntervalUnit, IntervalHandler> intervalHandlerMap = new HashMap<>();

    /**
     * 构造函数
     */
    public OracleIntervalAdjust() {
        DefaultIntervalHandler defaultIntervalHandler = new DefaultIntervalHandler();
        intervalHandlerMap.put(SQLIntervalUnit.YEAR, defaultIntervalHandler);
        intervalHandlerMap.put(SQLIntervalUnit.YEAR_MONTH, new YearMonth2MonthIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.QUARTER, new Quarter2MonthIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.MONTH, defaultIntervalHandler);
        intervalHandlerMap.put(SQLIntervalUnit.WEEK, new Week2DayIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.DAY, defaultIntervalHandler);
        intervalHandlerMap.put(SQLIntervalUnit.DAY_HOUR, new DayHour2HourIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.DAY_MINUTE, new DayMinute2MinuteIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.DAY_SECOND, new DaySecond2SecondIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.DAY_MICROSECOND, new DayMicroSecond2MicroSecondIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.HOUR, defaultIntervalHandler);
        intervalHandlerMap.put(SQLIntervalUnit.HOUR_MINUTE, new HourMinute2MinuteIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.HOUR_SECOND, new HourSecond2SecondIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.HOUR_MICROSECOND, new HourMicroSecond2MicroSecondIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.MINUTE, defaultIntervalHandler);
        intervalHandlerMap.put(SQLIntervalUnit.MINUTE_SECOND, new MinuteSecond2SecondIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.MINUTE_MICROSECOND, new MinuteMicroSecond2MicroSecondIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.SECOND, defaultIntervalHandler);
        intervalHandlerMap.put(SQLIntervalUnit.SECOND_MICROSECOND, new SecondMicroSecond2MicroSecondIntervalHandler());
        intervalHandlerMap.put(SQLIntervalUnit.MICROSECOND, defaultIntervalHandler);

    }

    /**
     * 转换 with recursive
     *
     * @param x x
     */
    @Override
    public void adjust(SQLIntervalExpr x) {
        IntervalHandler intervalHandler = intervalHandlerMap.get(x.getUnit());
        intervalHandler.interval(x);
    }
}
